/**
 * Created by ZHOUSH on 3/3/2016.
 */
app.directive('stack', ['$rootScope', '$timeout', 'layoutManager',
    function ($rootScope, $timeout, layoutManager) {
        return {
            restrict: 'A',
            link: {
                pre: function ($scope, $element, $attrs) {
                    $scope.doLayout = function () {
                        var type = $attrs['stack'];
                        var $parent = $($element.parent());
                        var parentHeight = $parent.height();
                        var $elem = $($element);
                        var ePos = $elem.offset();
                        ePos.right = ePos.left + $elem.outerWidth(true);
                        ePos.bottom = ePos.left + $elem.outerHeight(true);
                        var $brother = $parent.children();
                        var in_column = [], i = 0, $item;
                        for (i = 0; i < $brother.length; i++) {
                            $item = $($brother[i]);
                            if ($item.is($element)) {
                                continue;
                            }
                            var pos = $item.offset();
                            pos.right = pos.left + $item.outerWidth(true);
                            pos.bottom = pos.left + $item.outerHeight(true);
                            if (pos.left - ePos.right < -1 && pos.right - ePos.left > 1) {
                                var css_pos = $item.css('position');
                                var hidden = $item.is(':hidden');
                                if (['fixed'].indexOf(css_pos) < 0 && !hidden) {
                                    in_column.push($item);
                                }
                            }
                        }
                        var height = 0;
                        for (i = 0; i < in_column.length; i++) {
                            $item = $(in_column[i]);
                            height += $item.outerHeight(true);
                        }
                        if ($(window).outerWidth(true) < 768) {
                            $elem.outerHeight('auto');
                        } else {
                            $elem.outerHeight(parentHeight - height);
                        }
                    };
                    layoutManager.push($scope.doLayout);
                    $(window).on('resize', $scope.doLayout);
                    $scope.$on('layoutChanged', $scope.doLayout);
                },
                post: function ($scope) {
                }
            }
        }
    }
]).directive('client', ['$rootScope', '$timeout', 'layoutManager',
    function ($rootScope, $timeout, layoutManager) {
        return {
            restrict: 'A',
            link: {
                pre: function () {
                    $(document).on('ready', function () {
                        $timeout(function () {
                            layoutManager.layout();
                        })
                    })
                }
            }
        }
    }
]);